package com.sc.nft.controller;




import cn.hutool.core.util.StrUtil;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.dto.*;
import com.sc.nft.entity.vo.DaoWhetherAttentionVO;
import com.sc.nft.entity.vo.WhetherAttentionVO;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.DaoAttentionService;
import com.sc.nft.sup.PageRequest;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * dao关注(DaoAttention)表控制层
 *
 * @author 王伟成
 * @since 2024-03-06 17:05:28
 */

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("daoAttention")
@Api(tags = "dao关注")
public class DaoAttentionController {
    /**
     * 服务对象
     */
    private final DaoAttentionService daoAttentionService;
    private final Redisson redisson;


    @ApiOperation(value = "关注或取消关注", notes = "关注或取消关注", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/attentionOrCancel", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result attention(@RequestBody DaoAttentionOrCancelDTO request) {
        UserInfo user = UserPool.getUser();
        String key= StrUtil.format("{}{}", RedisKeyEnum.DAO_ATTENTION.getKey(),user.getId());
        RLock lock = redisson.getLock(key);
        try{
            boolean b = lock.tryLock(3, TimeUnit.SECONDS);
            if (!b) {
                throw new GlobalRunTimeException("请求频率过高");
            }
            daoAttentionService.attentionOrCancel(user,request);
        }catch (Exception e){
            e.printStackTrace();
            throw new GlobalRunTimeException("系统繁忙，请稍后再试");
        }finally {
            if (lock.isLocked()) {
                lock.unlock();
            }
        }
        return Result.success();
    }

    @ApiOperation(value = "用户是否关注当前DAO", notes = "用户是否关注当前DAO", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/whetherAttention", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<DaoWhetherAttentionVO> whetherAttention(@RequestBody DaoWhetherAttentionDTO request) {
        UserInfo user = UserPool.getUser();
        return Result.success(daoAttentionService.whetherAttention(user,request));
    }

    @ApiOperation(value = "关注列表", notes = "关注列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/attentionList", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<DaoAttentionListVO> attentionList(@RequestBody PageRequest pageRequest) {
        UserInfo user = UserPool.getUser();
        return PageResult.success(daoAttentionService.attentionList(user,pageRequest));
    }
}
